import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import './components/common/directives';
import 'babel-polyfill';
import API from './api/index'
import TMP from './utils/tools'
import store from './store'  //引入store
import BASEURL from './utils/helper'
import JsonExcel from 'vue-json-excel'
import axios from 'axios';
import echarts from 'echarts'
import './plugins/element.js'

Vue.prototype.$echarts = echarts

Vue.component('downloadExcel', JsonExcel)

Vue.config.productionTip = false;
Vue.use(VueI18n);
Vue.use(API).use(TMP);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

axios.defaults.withCredentials = true;

// 动态修改浏览器导航栏图标和文字
export function changeIconTitle() {
	const changeFavicon = link => {
		let $favicon = document.querySelector('link[rel="icon"]');
		if ($favicon !== null) {
			$favicon.href = link;
		} else {
			$favicon = document.createElement("link");
			$favicon.rel = "icon";
			$favicon.href = link;
			document.head.appendChild($favicon);
		}
	};
	let iconUrl = "https://ts1.cn.mm.bing.net/th/id/R-C.5d51a6ed72b8cbc791fc67a5831ded13?rik=TACSKky%2fiy34gg&riu=http%3a%2f%2fwww.kuaipng.com%2fUploads%2fpic%2fw%2f2020%2f04-27%2f79244%2fwater_79244_698_698_.png&ehk=GXLI6vakbADxNykMvBeQLpsmeUrB7EeEP%2f1%2b1PUolIM%3d&risl=&pid=ImgRaw&r=0"; // 图片地址
	changeFavicon(iconUrl); // 动态修改网站图标
}

Vue.prototype.$baseurl = BASEURL;

Date.prototype.format =  function (fmt) {
    let o = {
      "M+" : this.getMonth()+1,                 //月份
      "d+" : this.getDate(),                    //日
      "H+" : this.getHours(),                   //小时
      "m+" : this.getMinutes(),                 //分
      "s+" : this.getSeconds(),                 //秒
      "q+" : Math.floor((this.getMonth()+3)/3), //季度
      "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
      fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(let k in o) {
      if(new RegExp("("+ k +")").test(fmt)){
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
      }
    }
    return fmt;
  };
  

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    changeIconTitle()
    document.title = `${to.meta.title} | seu-quiz-system`;
    var role = localStorage.getItem('ms_username');
    if (role == null) role = 'user';
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin' ? next() : next('/403');
    } else {
        // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
        if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
            Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
                confirmButtonText: '确定'
            });
        } else {
            next();
        }
    }
});

new Vue({
    router,
    i18n,
    store,
    render: h => h(App)
}).$mount('#app');
